package com.sms.utils;

import java.util.UUID;

import com.alibaba.fastjson.JSON;
import com.sms.cache.CacheManager;
import com.sms.config.ResourceUtil;
import com.sms.web.system.entity.SysUser;

/**
 * @desc 
 * @author WuJiangWu
 * @date 2018年7月23日下午9:32:19
 */
public class TokenUtils {
	
	//获取token有效期参数
    private static int expires = Integer.parseInt(ResourceUtil.getConfigByName("token.expires"));

    public static String token() {
        return UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
    }

    public static void putUser(String token, SysUser user) {
        CacheManager.getCacheProvider().put(token, "user", user, expires);
    }

    public static SysUser getUser(String token) {
        if (org.apache.commons.lang3.StringUtils.isBlank(token))
            throw new IllegalArgumentException("token is null");
        String userjson = (String) CacheManager.getCacheProvider().get(token, "user", expires);
        if (userjson == null)
            return null;
        return JSON.parseObject(userjson, SysUser.class);
    }

    public static void put(String token, String key, Object data) {
        CacheManager.getCacheProvider().put(token, key, data, expires);
    }

    public static Object get(String token, String key) {
        return CacheManager.getCacheProvider().get(token, key, expires);
    }

    public static void remove(String token){
        CacheManager.getCacheProvider().remove(token);
    }
    public static void remove(String token,String key){
        CacheManager.getCacheProvider().remove(token,key);
    }

}
